package com.mall.b2c.domain.delivery;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;

import com.mall.common.domain.PriorityEntity;

/**
 * 配送方式
 * 
 * @author lzx
 *
 */
@Entity
@Table(name = "b2c_delivery_mehtod")
public class DeliveryMethodDomain extends PriorityEntity {
	private static final long serialVersionUID = -8475275470735106832L;
	
	private String name; // 名称
	private String company; // 所属公司
	private String code; // 代码
	private String description; // 描述
	private String icon; // 图标
	private BigDecimal firstPrice; // 首重价格
	private BigDecimal firstWeight; // 首重
	private BigDecimal continuePrice; // 续重价格
	private BigDecimal continueWeight; // 续重
	private Boolean isAvaliable; //是否可用
	private String config;
	private Boolean isDefault; //是否默认配送方式

	@PrePersist
	public void prePersist(){
		if(isDefault == null){
			isDefault = true;
		}
	}

	/**
	 * 获取区域配置
	 * @return
	 */
	@Transient
	public JSONObject getConfigJson(){
		if(StringUtils.isBlank(config)){
			return null;
		}
		return JSONObject.fromObject(config);
	}
	
	/**
	 * 获取配送费用
	 * @param weight
	 * @return
	 */
	@Transient
	public BigDecimal getShipFee(Long areaId, BigDecimal weight){
		BigDecimal fw = firstWeight;
		BigDecimal fp = firstPrice;
		BigDecimal cw = continueWeight;
		BigDecimal cp = continuePrice;
		if(areaId == null || weight == null){
			return null;
		}
		boolean flag = true;
		JSONObject jObj = getConfigJson();
		if(jObj != null){
			for(Object key : jObj.keySet()){
				if(key.toString().contains("," + areaId + ",")){
					JSONObject detail = jObj.getJSONObject(key.toString());
					fw = new BigDecimal(detail.getString("firstWeight"));
					fp = new BigDecimal(detail.getString("firstPrice"));
					cw = new BigDecimal(detail.getString("continueWeight"));
					cp = new BigDecimal(detail.getString("continuePrice"));
					flag = false;
					break;
				}
			}
		}
		if(flag){
			return null;
		}
		if(weight.compareTo(fw) <= 0){
			return fp;
		}
		BigDecimal count = weight.subtract(fw).divide(cw, 0, BigDecimal.ROUND_UP);
		return fp.add(cp.multiply(count));
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public BigDecimal getFirstPrice() {
		return firstPrice;
	}

	public void setFirstPrice(BigDecimal firstPrice) {
		this.firstPrice = firstPrice;
	}

	public BigDecimal getFirstWeight() {
		return firstWeight;
	}

	public void setFirstWeight(BigDecimal firstWeight) {
		this.firstWeight = firstWeight;
	}

	public BigDecimal getContinuePrice() {
		return continuePrice;
	}

	public void setContinuePrice(BigDecimal continuePrice) {
		this.continuePrice = continuePrice;
	}

	public BigDecimal getContinueWeight() {
		return continueWeight;
	}

	public void setContinueWeight(BigDecimal continueWeight) {
		this.continueWeight = continueWeight;
	}

	@Column(length=1000)
	public String getConfig() {
		return config;
	}

	public void setConfig(String config) {
		this.config = config;
	}

	public Boolean getIsAvaliable() {
		return isAvaliable;
	}

	public void setIsAvaliable(Boolean isAvaliable) {
		this.isAvaliable = isAvaliable;
	}

	public Boolean getIsDefault() {
		return isDefault;
	}

	public void setIsDefault(Boolean isDefault) {
		this.isDefault = isDefault;
	}
}
